package cn.edu.jxau.util;

import java.util.Iterator;
import java.util.NoSuchElementException;

public class LinkedList<T> extends List<T> {

    /**
     * 列表元素个数
     */
    private int size;

    /**
     * 列表头指针
     */
    private Node<T> first;

    public LinkedList() {
        first = new Node<>(); // 有头结点
    }

    @Override
    public void remove(T item) {

        Node<T> cursor = first;
        while (cursor.next != null) {
            if (cursor.next.item == null) {
                if (item == null) {
                    cursor.next = cursor.next.next;
                    size--;
                    return ;
                }
            } else {
                if (cursor.next.item.equals(item)) {
                    cursor.next = cursor.next.next;
                    size--;
                    return ;
                }
            }
            cursor = cursor.next;
        }
    }
    
    public void removeAll(T item) {
        
        Node<T> cursor = first;
        while (cursor.next != null) {
            if (cursor.next.item == null) {
                if (item == null) {
                    cursor.next = cursor.next.next;
                    size--;
                    continue;
                }
            } else {
                if (cursor.next.item.equals(item)) {
                    cursor.next = cursor.next.next;
                    size--;
                    continue;
                }
            }
            cursor = cursor.next;
        }
    }

    private class Node<T> {
        private T item;
        private Node<T> next;

        public Node() {

        }

        public Node(T item) {
            this.item = item;
        }
    }
}
